<template>
  <view>
    <view class="boxs">
      <view class="forms">
        <u-form
          :label-width="130"
          label-position="left"
          :model="form"
          ref="uForm"
        >
          <!-- <u-form-item label="公司名称" prop="name">
            <u-input
              :disabled="realnameState"
              :border="border"
              placeholder="请输入您的公司名称"
              v-model="form.name"
            />
          </u-form-item> -->
		  <u-form-item label="公司名称" prop="name">
		    <u-input
		      
		      :border="border"
		      placeholder="请输入您的公司名称"
		      v-model="form.name"
		    />
		  </u-form-item>
         
		  <u-form-item :label-position="labelPosition" label="公司规模" prop="scale" label-width="150">
		  	<u-input :border="border" type="select"  :select-open="scaleShow" v-model="form.scale"
		  		placeholder="请选择您的公司规模" @click="scaleShow = true"></u-input>
		  </u-form-item>
		  <u-select mode="single-column" :list="scaleList" v-model="scaleShow"
		  	@confirm="FnConfirm('scale', $event)"></u-select>
		 
          <u-form-item label="企业介绍" prop="introduce">
            <u-input
              
              :border="border"
              placeholder="请输入您的企业介绍"
              v-model="form.introduce"
              type="textarea"
              :height="height"
              :auto-height="autoHeight"
            />
          </u-form-item>
          <!-- <u-form-item ref="address" label="公司地址" prop="addr">
            <u-input
              @click="changeAddr"
              :disabled="realnameState"
              readonly="readonly"
              :border="border"
              placeholder="请选择公司地址"
              v-model="form.addr"
            />
          </u-form-item> -->

          	<u-form-item ref="address" label="公司地址" prop="addr">
						<u-input @click="chooseLocation"  readonly="readonly" :border="border"
							placeholder="请选择公司地址" v-model="form.addr" />
			</u-form-item>
			
			<u-form-item label="营业执照" prop="certImgUrls">
			  <u-upload
			    :before-upload="beforeUpload"
			   
			    width="155"
			    multiple
			    :maxCount="certImgUrlsLen"
			    ref="uUpload2"
			    :show-progress="false"
			    height="155"
			    action="https://recruit-api.xujiekeji.com/recruit-server/app/file/upload"
			    :auto-upload="true"
			    @on-remove="onRemove2"
			    :file-list="fileList2"
			    @on-success="successUpload2"
			  >
			  </u-upload>
			  <!-- <image style="width: 80px; height: 80px; margin: 0 6px 0 0; " v-for="(item,index) in fileList2" :key="index" :src="item.url" @click="clickImg2(index)" mode=""></image> -->
			</u-form-item>


       

          <u-form-item label="公司logo" prop="logoUrl">
            <u-upload
              
              width="155"
              multiple
              :maxCount="1"
              ref="uUpload2"
              :show-progress="false"
              height="155"
              action="https://recruit-api.xujiekeji.com/recruit-server/app/file/upload"
              :auto-upload="true"
              @on-remove="onRemove"
              :file-list="fileList0"
              @on-success="successUpload"
            >
            </u-upload>
			<view class="" v-for="(item,index) in fileList0" :key="index">
				<!-- <image style="width: 80px; height:80px; margin: 0 6px 0 0;"  :src="item.url" @click="clickImg0(index)" mode=""></image> -->
			</view>
          </u-form-item>
          <u-form-item label="办公图片" prop="descImgUrls">
            <u-upload
             
              width="155"
              multiple
              :maxCount="descImgUrlsLen"
              ref="uUpload"
              :show-progress="false"
              height="155"
              action="https://recruit-api.xujiekeji.com/recruit-server/app/file/upload"
              :auto-upload="true"
              @on-remove="onRemove1"
              :file-list="fileList1"
              @on-success="successUpload1"
            >
            </u-upload>
			<!-- <image style="width: 80px; height: 80px; margin: 0 6px 0 0;" v-for="(item,index) in fileList1" :key="index" :src="item.url" @click="clickImg1(index)" mode=""></image> -->
          </u-form-item>
          
        </u-form>
      </view>
	  <!-- v-if="!realnameState" -->
      <view v-if="!realnameState"  class="btnBox">
        <u-button
          ripple-bg-color="#5a8cdf"
          shape="circle"
          :ripple="true"
          class="btn"
          type="primary"
          @click="submit"
          >提交</u-button
        >
      </view>
	  
	  <view v-if="realnameState"  class="btnBox">
	    <u-button
	      ripple-bg-color="#5a8cdf"
	      shape="circle"
	      :ripple="true"
	      class="btn"
	      type="warning"
	      @click="edit"
	      >修改</u-button
	    >
	  </view>
	  
	  
    </view>
  </view>
</template>

<script>

import api from "../../../../api/routes.js";
import { mapGetters } from "vuex";
export default {
  components: {
 
  },
  computed: {
    // ...mapGetters(["sendadderss"]),
  },

  data() {
    return {
	  scaleShow:false,
      params: "",
      // 公司地址
      sendadderss: {},
      // 公司地址
      height: 120,
      autoHeight: true,
      id: undefined,
      hasaddr: true,
      descImgUrlsLen: 9,
      certImgUrlsLen: 9,
      realnameState: false,
      border: false,
	  
      fileList0: [],
      fileList00: [],
      fileList1: [],
      fileList11: [],
      fileList2: [],
      fileList22: [],
	  scaleList:[],
      form: {
        name: "",
        addr: "",
        adderss: {},
        fileList0: "",
        fileList1: "",
        fileList2: "",
        scale: "",
        introduce: "",
      },
      rules: {
			name: [
			  {
				required: true,
				message: "请输入您的企业名称",
				trigger: ["blur", "change"],
			  },
			],

			scale: [
			  {
				required: true,
				message: "请输入您的企业规模",
				trigger: ["blur", "change"],
			  },
			],
			introduce: [
			  {
				required: true,
				message: "请输入您的企业介绍",
				trigger: ["blur", "change"],
			  },
			],
			addr: [
			  {
				required: true,
				message: "请选择您的公司地址",
				trigger: ["blur", "change"],
			  },
			],
      
        
      },
    };
  },

  onLoad() {
    this.companyInfo();
	this.getdictionary('scale')
  },

  onShow() {
    // let that = this;
    // uni.$on("updateData", function (data) {
    
    //   console.log("监听到事件来自 updateData ，携带参数为：" + data);
      

    //   that.sendadderss = data;
    //   let { pname, cityname, adname, address } = data;
    //   that.form.addr = address;
    //   console.log( that.sendadderss, 3333333333333);
    // });
  },

  methods: {
	  // 点击浏览图片
	  clickImg0(index){
		let imgs = this.fileList0.map( item =>{
			//只返回图片路径
			return item.url  
		})

	  	 uni.previewImage({
	  	    urls: imgs, //需要预览的图片http链接列表，注意是数组
	  	    current: index, // 当前显示图片的http链接，默认是第一个
	  		
	  	  })
	  	
	  },
	  clickImg1(index){
		let imgs = this.fileList1.map( item =>{
			//只返回图片路径
			return item.url  
		})
	  	 uni.previewImage({
	  	    urls: imgs, //需要预览的图片http链接列表，注意是数组
	  	    current: index, // 当前显示图片的http链接，默认是第一个
	  		
	  	  })
	  	
	  },
	  clickImg2(index){
		let imgs = this.fileList2.map( item =>{
			//只返回图片路径
			return item.url  
		})
	  	 uni.previewImage({
	  	    urls: imgs, //需要预览的图片http链接列表，注意是数组
	  	    current: index, // 当前显示图片的http链接，默认是第一个
	  		
	  	  })
	  	
	  },
	  async getdictionary(code) {
	  	const res1 = await this.$myRequest({
	  		url: api.dictionary1,
	  		method: "GET",
	  		data: {
	  			code
	  		},
	  	});
	  	console.log(res1,1228);
	  	
	  	const lists = res1?.data?.list?.map(s => {
	  		return {
	  			value: s.dict_value,
	  			label: s.dict_label
	  		}
	  	})
	  	this[`${code}List`] = lists
	  },
	  FnConfirm(val, name, type) {
	  
	  	if (type) {
	  		this.form[type] = name[0].value
	  	} else {
	  		this.form[val] = name[0].value
	  	}
	  	
	  	
	  },

    	chooseLocation() {
				uni.chooseLocation({
					success: (data) => {
						let {
							address,
							latitude,
							longitude
						} = data;

						var reg = /.+?(省|市|自治区|自治州|县|区)/g // 省市区的正则

						let addr = address.match(reg);

						let list = ['北京', '天津', '上海', '重庆', '香港', '澳门']

						for (let i in list) {

							if (addr.toString().includes(list[i])) {
								addr.unshift(list[i] + '省')
							}

						}

						this.sendadderss = {
							pname: addr[0],
							cityname: addr[1],
							adname: addr[2],
							address,
							longitude,
							latitude
						};
						this.form.addr = address
			 	}
				})
			},


    // 文件超出大小限制
    beforeUpload(file, lists, name) {
      console.log(file, lists, name);
      // uni.showToast({
      // 	title: "图片最大不能超过2M",
      // 	icon: 'none'
      // })
    },
    successUpload(data, index, lists, name) {
      let url = data.url;
      this.fileList00.push(url);
      this.form.fileList0 = JSON.stringify(this.fileList00);
    },
    onRemove(index) {
      this.fileList00.splice(index, 1);
      this.form.fileList0 = JSON.stringify(this.fileList00);
    },

    successUpload1(data, index, lists) {
      let url = data.url;
      this.fileList11.push(url);
      this.form.fileList1 = JSON.stringify(this.fileList11);
    },
    onRemove1(index) {
      this.fileList11.splice(index, 1);
      this.form.fileList1 = JSON.stringify(this.fileList11);
    },
    successUpload2(data, index, lists, name) {
      let url = data.url;
      this.fileList22.push(url);
      this.form.fileList2 = JSON.stringify(this.fileList22);
    },
    onRemove2(index) {
      this.fileList22.splice(index, 1);
      this.form.fileList2 = JSON.stringify(this.fileList22);
    },


    // 企业认证接口
    async companyInfo() {
      const res = await this.$myRequest({
        url: api.companyInfo,
        method: "GET",
        data: {},
      });
      console.log(res.data,'7979');
	  
      this.hasaddr = false;
      let {
        district,
        city,
        province,
        lat,
        lgt,
        name,
        scale,
        introduce,
        address,
        certImgUrls,
        logoUrl,
        descImgUrls,
        id,
        auditState,
      } = res.data.company;
	  
	  
	  console.log(res,'####');
	  

      // auditState = '审核中'  //记得删掉

      this.realnameState = auditState == "已通过" ? true : false;

      this.id = id;
      this.form.name = name;
      this.form.scale = scale;
      this.form.introduce = introduce;
      this.form.addr = address;

      this.fileList0 = logoUrl.split(",").map((i) => {
        return {
          url: i,
        };
      });
      this.fileList00 = logoUrl.split(",");
      this.form.fileList0 = JSON.stringify(logoUrl.split(","));
      this.fileList1 = descImgUrls.split(",").map((i) => {
        return {
          url: i,
        };
      });
      this.fileList11 = descImgUrls.split(",");
      this.form.fileList1 = JSON.stringify(descImgUrls?.split(","));

      this.descImgUrlsLen = this.realnameState ? this.fileList11.length : 9;

      this.fileList2 = certImgUrls?.split(",").map((i) => {
        return {
          url: i,
        };
      });
	  console.log(this.fileList2,"this.fileList2");
      this.fileList22 = certImgUrls?.split(",");
	  console.log(this.fileList2,"this.fileList22");
      this.form.fileList2 = JSON.stringify(certImgUrls?.split(","));
	  console.log(this.fileList2,"this.form.fileList2");
      this.certImgUrlsLen = this.realnameState ? this.fileList22.length : 9;

      this.sendadderss = {
        pname: province,
        cityname: city,
        adname: district,
        address: address,
        longitude: lgt,
        latitude: lat,
      };
    },

    async memberCert() {
      let { pname, cityname, adname, address, longitude, latitude } =this.sendadderss;
      const res = await this.$myRequest({
			url: api.companyCert,
			method: "POST",
			data: {
			  id: this.id,
			  "certType":"其他",
			  name: this.form.name,
			  introduce: this.form.introduce,
			  scale: this.form.scale,
			  descImgUrls: JSON.parse(this.form.fileList1).join(","),
			  certImgUrls: this.form.fileList2
				? JSON.parse(this.form.fileList2).join(",")
				: "",
			  logoUrl: JSON.parse(this.form.fileList0).join(","),
			  province: pname,
			  city: cityname,
			  district: adname,
			  address: address,
			  lgt: longitude,
			  lat: latitude,
			},
      });

      if (res.data.code == 0) {
        uni.showToast({
          title: res.data.msg,
          duration: 2000,
        });
        setTimeout(() => {

		  wx.navigateBack({
		    delta: 1
		  })
		  wx.hideLoading()
        }, 1500);
      }
    },
	edit(){
		console.log('修改111');
		this.$refs.uForm.validate((valid) => {
		    if (valid) {
					if (this.fileList22.length <= 0) {
						uni.showToast({
						  icon: "none",
						  title: "请上传企业营业执照",
						});
						return;
					}
					  
					if (this.fileList00.length <= 0) {
						  uni.showToast({
							icon: "none",
							title: "请上传公司logo",
						  });
						  return;
					}
						
					if (this.fileList11.length <= 0) {
							uni.showToast({
							  icon: "none",
							  title: "请上传办公图片",
							});
							return;
					}
					this.memberCert();
			} else {
			  console.log("验证失败");
			}
		});
	},
    submit() {
      console.log(this.form);

      this.$refs.uForm.validate((valid) => {
        if (valid) {
          // if (this.form.addr == "") {
          //   uni.showToast({
          //     icon: "none",
          //     title: "请填写企业地址",
          //   });
          //   return;
          // }

          
      // if (this.form.addr=='') {
      //     uni.showToast({
      //       icon: "none",
      //       title: "请选择招工地点",
      //     });
      //     return;
      //   }


        if (this.fileList22.length <= 0) {
            uni.showToast({
              icon: "none",
              title: "请上传企业营业执照",
            });
            return;
          }
		  
		  if (this.fileList00.length <= 0) {
		      uni.showToast({
		        icon: "none",
		        title: "请上传公司logo",
		      });
		      return;
		    }
			
			if (this.fileList11.length <= 0) {
			    uni.showToast({
			      icon: "none",
			      title: "请上传办公图片",
			    });
			    return;
			}

         

          console.log("验证通过");

          this.memberCert();
        } else {
          console.log("验证失败");
        }
      });
    },
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
};
</script>
<style>
page {
  background: rgb(245, 247, 249);
}
</style>
<style lang="scss" scoped>
.boxs {
  .forms {
    background: #ffffff;
    display: block;
    padding: 0 3%;
    margin: 20rpx 3% 0;
    border-radius: 3px;
  }
}

.btnBox {
  width: 94%;
  margin: 40rpx 3%;
  padding-bottom: 50px;
}

.btn {
  background: $my-primary;
}
</style>
